﻿using Microsoft.AspNetCore.Mvc;
using WCS_Entity.Dto;
using WCS_Entity.Dto.TaskDto;
using WCS_Entity.Dto.UserDto;
using WCS_Service.Interface;

namespace WCS.Controllers
{
    /// <summary>
    /// 任务管理接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class TaskController : ControllerBase
    {
        private readonly ITaskService _taskService;
        public TaskController(ITaskService taskService)
        {
            _taskService = taskService;
        }
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreateTaskAsync([FromBody] CreatTaskDto dto)
        {
            WebApiCallBack result = new();
            var info = await _taskService.CreateTaskAsync(dto);
            if (info.taskInfo is null)
            {
                result.status = false;
                result.msg = info.ErrMsg;
                return result;
            }
            result.data = info.taskInfo;
            return result;
        }
        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateTaskAsync([FromBody] UpdateTaskDto dto)
        {
            WebApiCallBack result = new();
            var info = await _taskService.UpdateTaskAsync(dto);
            if (info is null)
            {
                result.status = false;
                result.msg = "更新任务失败";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 查询指定任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryTaskAsync([FromBody] int id)
        {
            WebApiCallBack result = new();
            var info = await _taskService.QueryTaskAsync(id);
            if (info is null)
            {
                result.status = false;
                result.msg = $"查询的任务{id}不存在";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 查询所有任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllTaskAsync()
        {
            WebApiCallBack result = new();
            var info = await _taskService.QueryAllTaskAsync();
            if (info is null)
            {
                result.status = false;
                result.msg = $"任务不存在";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 根据任务类型查询任务
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryTaskBaseTypeAsync([FromBody] string type)
        {
            WebApiCallBack result = new();
            var info = await _taskService.QueryTaskBaseTypeAsync(type);
            if (info is null || !info.Any())
            {
                result.status = false;
                result.msg = $"{type}的任务不存在";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 删除指定任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> DeleteTaskAsync([FromBody] int id)
        {
            WebApiCallBack result = new();
            result.status = await _taskService.DeleteTaskAsync(id);
            result.msg = result.status ? "任务删除成功" : "任务删除失败";
            return result;
        }
        /// <summary>
        /// 查询完成的任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryCompleteTaskAsync()
        {
            WebApiCallBack result = new();
            var info = await _taskService.QueryCompleteTaskAsync();
            if (info is null || !info.Any())
            {
                result.status = false;
                result.msg = $"完成的任务不存在";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 查询异常的任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryErrTaskAsync()
        {
            WebApiCallBack result = new();
            var info = await _taskService.QueryErrTaskAsync();
            if (info is null || !info.Any())
            {
                result.status = false;
                result.msg = $"异常的任务不存在";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 查询未执行的指定类型任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryWaitTaskAsync([FromBody] string type)
        {
            WebApiCallBack result = new();
            var info = await _taskService.QueryWaitTaskAsync(type,true);
            if (info is null || !info.Any())
            {
                result.status = false;
                result.msg = $"等待的任务不存在";
                return result;
            }
            result.data = info;
            return result;
        }
        /// <summary>
        /// 更新指定任务的状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateTaskStateAsync([FromBody] UpdateTaskStateDto dto)
        {
            WebApiCallBack result = new();
            result.status = await _taskService.UpdateTaskStateAsync(dto);
            result.msg = result.status ? "任务状态更新成功" : "任务状态更新失败";
            return result;
        }
    }
}
